<?php
//Set up the system
require('config.include.php');
//Auth stuff
require('auth.include.php');
//Approve/Unapprove
if(isset($_GET['p'])&&$_p->tb_comments->idExists($_GET['p'])) {
	if(isset($_GET['approve'])) {
		$_p->approveComment($_GET['p']);
	}
	elseif(isset($_GET['unapprove'])) {
		$_p->unapproveComment($_GET['p']);
	}
	elseif(isset($_GET['confirmdelete'])) {
		$_p->deleteComment($_GET['p']);
	}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Manage Comments</title>
<link rel="stylesheet" href="admin.css" />
</head>

<body>
<div id="wrapper">
	<div id="header">
    	<?php echo('<a href="../" class="breadcrumb">'.SITE_TITLE.'</a> &raquo; <a href="dashboard.php" class="breadcrumb">Dashboard</a> &raquo; <a href="managecomments.php" class="breadcrumb">Manage Comments</a>'); ?>
    </div>
    <div id="sidebar">
<?php
	require('nav.include.php');
?>
    </div>
    <div id="content">
        <div class="contentblock">
<?php
	echo('<h2>Manage Comments</h2>');
	$show_unapproved=isset($_REQUEST['show_unapproved']);
	$comments=fetch::sortSelection($_p->tb_comments->select($show_unapproved?'!%approved%':'true'),'timestamp',SORT_DESC);
	//Decide what page is showing
	$p=isset($_REQUEST['commentp'])?$_REQUEST['commentp']:0;
	//The show/hide approved comments link
	if($show_unapproved)
		echo('<a href="managecomments.php">Unhide approved comments</a><br />');
	else
		echo('<a href="managecomments.php?show_unapproved">Show only unapproved comments</a><br />');
	if(count($comments)>0) {
		//Count pages
		$total_comments=count($comments);
		$pages=$total_comments/COMMENTS_PER_LIST;
		//If we can't divide cleanly, add one to the pages to cover the missing remainder because php will round down
		if($total_comments%COMMENTS_PER_LIST!=0)
			$pages=floor($pages)+1;
		echo('<div class="pagination">');
		//Pagination
		if($pages!=1&&$pages!=0) {
			$i=$p-PREVIOUS_PAGES;
			if($i<0)
				$i=0;
			if($p>0)
				echo('<a class="first" href="managecomments.php?'.($show_unapproved?'show_unapproved':'').'&amp;commentp=0">&laquo; First</a> ');
			if($p>1)
				echo('<a class="previous" href="managecomments.php?'.($show_unapproved?'show_unapproved':'').'&amp;commentp='.($p-1).'">Previous</a> ');
			for(;$i<$p;$i++)
				echo('<a class="last5" href="managecomments.php?'.($show_unapproved?'show_unapproved':'').'&amp;commentp='.$i.'">'.$i.'</a> ');
			echo('<a class="current"a href="managecomments.php?'.($show_unapproved?'show_unapproved':'').'&amp;commentp='.$p.'">'.$p.'</a> ');
			$i=$p+NEXT_PAGES+1;
			if($i>$pages)
				$i=$pages;
			for($y=$p+1;$y<$i;$y++)
				echo('<a class="next5" href="managecomments.php?'.($show_unapproved?'show_unapproved':'').'&amp;commentp='.$y.'">'.$y.'</a> ');
			if($p<$pages-2)
				echo('<a class="next" href="managecomments.php?'.($show_unapproved?'show_unapproved':'').'&amp;commentp='.($p+1).'">Next</a> ');
			if($p<$pages-1)
				echo('<a class="last" href="managecomments.php?'.($show_unapproved?'show_unapproved':'').'&amp;commentp='.($pages-1).'">Last ('.($pages-1).') &raquo;</a>');
		}
		echo('</div>');
		//Show comments on that page
		$startat=$p*COMMENTS_PER_LIST;
		$i=0;
		$y=COMMENTS_PER_LIST;
		echo('<ul>');
		foreach($comments as $pid => $d) {
			if($i==$startat) {
				if($y>0) {
					echo('<li><strong>'.($d['prominent']!==false?'['.$_p->tb_users->getFieldValue($d['prominent'],'username').']':$d['name']).'</strong> on '.date('j<\s\up>S</\s\up> M y',$d['timestamp']).' ('.$d['ip'].') said:<br />'.htmlentities($d['content'],ENT_QUOTES,"UTF-8").'<br />
					'.($d['approved']?
					'<a href="managecomments.php?'.($show_unapproved?'show_unapproved':'').'&amp;p='.$pid.'&amp;commentp='.$p.'&amp;unapprove">Unapprove</a>':
					'<a href="managecomments.php?'.($show_unapproved?'show_unapproved':'').'&amp;p='.$pid.'&amp;commentp='.$p.'&amp;approve">Approve</a>').' | 
					<a href="managecomments.php?'.($show_unapproved?'show_unapproved':'').'&amp;p='.$pid.'&amp;commentp='.$p.($_GET['p']==$pid&&isset($_GET['delete'])?'&amp;confirmdelete':'&amp;delete').'">'.
					(isset($_GET['delete'])&&$_GET['p']==$pid?'Confirm Delete':'Delete').'</a></li><br />');
					$y--;
				}
				else {
					break;
				}
			}
			else {
				$i++;
			}
		}
		echo('</ul>');
	}
	else {
		echo('No comments to display.');
	}
?>
		</div>
    </div>
    <div id="footer">
    </div>
</div>
</body>
</html>